﻿using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Core.Utils;
using PMS.Data.DbUtils;
using PMS.Data.Entities.ScheduleTasks;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PMS.Services.EntityServices.SchduleTasks
{
    /// <summary>
    /// Task service
    /// </summary>
    public partial class ScheduleTaskService : IScheduleTaskService
    {
        #region Fields

        private readonly IRepository<SystemScheduleTask> _taskRepository;

        #endregion

        #region Ctor

        public ScheduleTaskService(IRepository<SystemScheduleTask> taskRepository)
        {
            _taskRepository = taskRepository;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Deletes a task
        /// </summary>
        /// <param name="task">Task</param>
        public virtual void DeleteTask(SystemScheduleTask task)
        {
            if (task == null)
                throw new ArgumentNullException(nameof(task));

            _taskRepository.Delete(task);
        }

        /// <summary>
        /// Gets a task
        /// </summary>
        /// <param name="taskId">Task identifier</param>
        /// <returns>Task</returns>
        public virtual SystemScheduleTask GetTaskById(int taskId)
        {
            if (taskId == 0)
                return null;

            return _taskRepository.GetById(taskId);
        }

        /// <summary>
        /// Gets a task by its type
        /// </summary>
        /// <param name="type">Task type</param>
        /// <returns>Task</returns>
        public virtual SystemScheduleTask GetTaskByType(string type)
        {
            if (string.IsNullOrWhiteSpace(type))
                return null;

            var query = _taskRepository.Table;
            query = query.Where(st => st.Type == type);
            query = query.OrderByDescending(t => t.Id);

            var task = query.FirstOrDefault();
            return task;
        }

        /// <summary>
        /// Gets all tasks
        /// </summary>
        /// <param name="showHidden">A value indicating whether to show hidden records</param>
        /// <returns>Tasks</returns>
        public virtual IList<SystemScheduleTask> GetAllTasks(bool showHidden = false)
        {
            var query = _taskRepository.Table.OrderBy(t => t.GroupName);
            var tasks = query.ToList();
            if (!showHidden)
                return tasks.Where(t => t.Status != ScheduleJobActionStatus.Stop && t.Status != ScheduleJobActionStatus.Error).ToList();
            return tasks;
        }

        /// <summary>
        /// Gets SystemScheduleTasks by page
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="showHidden"></param>
        /// <returns></returns>
        public virtual IPagedList<SystemScheduleTask> GetPagedTasks(int pageIndex = 0, int pageSize = int.MaxValue, bool showHidden = false)
        {
            var query = _taskRepository.Table;
            if (!showHidden)
                query = query.Where(t => t.Status != ScheduleJobActionStatus.Stop && t.Status != ScheduleJobActionStatus.Error);
            query = query.OrderBy(t => t.GroupName);
            return new PagedList<SystemScheduleTask>(query, pageIndex, pageSize);
        }
        /// <summary>
        /// Inserts a task
        /// </summary>
        /// <param name="task">Task</param>
        public virtual void InsertTask(SystemScheduleTask task)
        {
            if (task == null)
                throw new ArgumentNullException(nameof(task));

            _taskRepository.Insert(task);
        }

        /// <summary>
        /// Updates the task
        /// </summary>
        /// <param name="task">Task</param>
        public virtual void UpdateTask(SystemScheduleTask task)
        {
            if (task == null)
                throw new ArgumentNullException(nameof(task));

            _taskRepository.Update(task);
        }

        #endregion
    }
}
